package com.fy.controller;

import com.fy.service.UserService;
import com.fy.pojo.User;
import com.fy.pojo.UserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("list")
    public String list(UserVo userVo,Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<User> list = userService.list(userVo, pageNum, pageSize);
        model.addAttribute("pg",list);
        model.addAttribute("userVo",userVo);
        return "list";
    }
    @ResponseBody
    @RequestMapping("del")
    public String del(Integer id){
        int i = userService.delete(id);
        return "true";
    }
    @RequestMapping("toUpdate")
    public String toUpdate(Integer id,Model model){
        User user = userService.selectOne(id);
        model.addAttribute("user",user);
        return "update";
    }
    @RequestMapping("update")
    public String update(@Validated User user, BindingResult result,Model model){
        if(result.hasErrors()){
            model.addAttribute("error","不能为空");
            //return "update";
        }
//        if(!user.getTelephone().matches("//d{11}")){
//            model.addAttribute("telephone","手机号只能是数字");
//            return "update";
//        }
        int i = userService.update(user);
        return "redirect:/list";
    }
}
